package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsDto;
import com.guigu.erp.dto.ManufactureDesignProcedureDto;
import com.guigu.erp.dto.ManufactureDesignProcedureModuleDto;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/manufacture/process")
@Controller
public class ManufactureDesignProcedureController {
    @Autowired
    private DesignProcedureService designProcedureService;

    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;


    //查询所有工序的请求
    @RequestMapping("queryProcess")
    @ResponseBody
    public List<DesignProcedure> queryProcess(){
        List<DesignProcedure> designProcedures = designProcedureService.list();
        return designProcedures;
    }

    //添加工序表，工序明细表，修改产品档案工序设计标志
    @RequestMapping("addProcess")
    @ResponseBody
    public boolean addProcess(@RequestBody ManufactureDesignProcedureDto dto){
        System.out.println(dto.getProcess());
        //生产工序设计单编号
        String designId = numberUtils.getTypeNumber("201");
        dto.setDesignId(designId);

        //设置工时总成本
        BigDecimal sum = new BigDecimal(0);
        List<ManufactureDesignProcedureDetails> process = dto.getProcess();
        for(int i=0;i<process.size();i++){
            BigDecimal totalPrice=process.get(i).getSubtotal();
            sum=sum.add(totalPrice);
        }
        dto.setCostPriceSum(sum);

        //设置审核标志  等待审核
        dto.setCheckTag("S001-0");
        //设置变更标志  未变更
        dto.setChangeTag("B002-0");
        //设置工序物料设计标志  未设计
        dto.setDesignModuleTag("G002-0");
        //设置工序物料变更标志  未变更
        dto.setDesignModuleChangeTag("G003-0");

        // 设置当前工序设计单的分类编号
        // 获取产品编号
        String productId = dto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        if (designFile != null) {
            dto.setFirstKindId(designFile.getFirstKindId());
            dto.setFirstKindName(designFile.getFirstKindName());
            dto.setSecondKindId(designFile.getSecondKindId());
            dto.setSecondKindName(designFile.getSecondKindName());
            dto.setThirdKindId(designFile.getThirdKindId());
            dto.setThirdKindName(designFile.getThirdKindName());
        }

        //调动业务层方法
        boolean flag = manufactureDesignProcedureService.addManufactureProcess(dto);

        return flag;
    }

    //查询工序设计单未审核的设计单
    @RequestMapping("queryUnDesigned")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> queryUnDesigned(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        //查询条件   S001-0: 等待审核
        wrapper.eq("CHECK_TAG","S001-0");
        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureService.list(wrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    //根据id查询工序设计单信息
    @RequestMapping("getIdProcess")
    @ResponseBody
    public ManufactureDesignProcedure getIdProcess(Integer id){
        ManufactureDesignProcedure procedure = manufactureDesignProcedureService.getById(id);
        return procedure;
    }

    //根据id查询工序设计明细
    @RequestMapping("getByIdProcessDetails")
    @ResponseBody
    private List<ManufactureDesignProcedureDetails> getByIdProcessDetails(Integer id){
        QueryWrapper<ManufactureDesignProcedureDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(wrapper);
        return list;
    }

    //修改工序设计单审核状态
    @RequestMapping("updateProcessAudit")
    @ResponseBody
    public boolean updateProcessAudit(ManufactureDesignProcedure manufactureDesignProcedure){
        boolean flag = manufactureDesignProcedureService.updateById(manufactureDesignProcedure);
        return flag;
    }

    //查询所有工序设计单
    @RequestMapping("processInquiry")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> processInquiry(DesignModuleVo vo,Integer pageNo,Integer pageSize){
        PageUtils<ManufactureDesignProcedure> manufactureList = manufactureDesignProcedureService.getManufactureList(vo, pageNo, pageSize);
        return manufactureList;
    }

    //查询所有审核通过工序设计单的工序设计表
    @RequestMapping("unMakeProcess")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> unMakeProcess(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("CHANGE_TAG","B002-0");
        wrapper.eq("DESIGN_MODULE_TAG","G002-0");
        wrapper.eq("DESIGN_MODULE_CHANGE_TAG","G003-0");

        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureService.list(wrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> pageUtils=new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    //根据产品档案id查询是否设计物料
    @RequestMapping("queryProductId")
    @ResponseBody
    public boolean queryProductId(String productId){
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",productId);
        DesignFile file = designFileService.getOne(wrapper);
        return !file.getDesignModuleTag().equals("W001-0");
    }

    //根据产品档案编号查询物料明细
    @RequestMapping("getModuleDetails")
    @ResponseBody
    public List<DesignModuleDetails> getModuleDetails(String productId){
        QueryWrapper<DesignModule> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",productId);
        DesignModule one = designModuleService.getOne(wrapper);
        Integer id = one.getId();
        QueryWrapper<DesignModuleDetails> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",id);
        List<DesignModuleDetails> list = designModuleDetailsService.list(queryWrapper);
        return list;
    }

    //根据产品编号查询生产工序编号
    @RequestMapping("getProductIdQueryId")
    @ResponseBody
    public ManufactureDesignProcedure getProductIdQueryId(String id){
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",id);
        ManufactureDesignProcedure designProcedure = manufactureDesignProcedureService.getOne(wrapper);
        return designProcedure;
    }

    //根据工序序号查询工序明细
    @RequestMapping("getIdQueryProcess")
    @ResponseBody
    public DesignProcedure getIdQueryProcess(Integer id){
        DesignProcedure procedureDetails = designProcedureService.getById(id);
        return procedureDetails;
    }

    //添加工序物料
    @RequestMapping("addOperationMaterial")
    @ResponseBody
    public boolean addOperationMaterial(@RequestBody ManufactureDesignProcedureDetailsDto dto){
        boolean flag = manufactureDesignProcedureModuleService.addProcessAndModule(dto);
        return flag;
    }

    //重新设计的工序物料明细查询
    @RequestMapping("operationItemInquiry")
    @ResponseBody
    public List<ManufactureDesignProcedureModule> operationItemInquiry(String productId,String procedureId){
        //根据产品编号查询工序设计单
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",productId);
        ManufactureDesignProcedure designProcedure = manufactureDesignProcedureService.getOne(wrapper);
        //得到工序设计单id和工序编号 查询工序设计明细表
        QueryWrapper<ManufactureDesignProcedureDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",designProcedure.getId());
        queryWrapper.eq("PROCEDURE_ID",procedureId);
        ManufactureDesignProcedureDetails procedureDetails = manufactureDesignProcedureDetailsService.getOne(queryWrapper);
        //得到工序设计明细的id 查询工序物料明细的信息
        QueryWrapper<ManufactureDesignProcedureModule> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.eq("PARENT_ID",procedureDetails.getId());
        List<ManufactureDesignProcedureModule> moduleList = manufactureDesignProcedureModuleService.list(moduleQueryWrapper);
        return moduleList;

    }

    //重新设计删除工序物料明细 ，修改设计标志 修改可用数量
    @RequestMapping("deleteOperationItemDetails")
    @ResponseBody
    public boolean deleteOperationItemDetails(@RequestBody ManufactureDesignProcedureModuleDto dto){
        boolean flag = manufactureDesignProcedureModuleService.removeOperationItemDetails(dto);
        return flag;
    }

    //工序物料设计单提交
    @RequestMapping("processMaterialSubmission")
    @ResponseBody
    public boolean processMaterialSubmission(@RequestBody ManufactureDesignProcedureDto dto){
        //根据工序设计单编号重新工序设计单信息
        String designId = dto.getDesignId();
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("DESIGN_ID",designId);
        ManufactureDesignProcedure procedure = manufactureDesignProcedureService.getOne(wrapper);
        //取出工序明细
        List<ManufactureDesignProcedureDetails> process = dto.getProcess();
        BigDecimal sum = new BigDecimal(0);
        for (int i=0;i<process.size();i++){
            BigDecimal temp=process.get(i).getModuleSubtotal();
            sum=sum.add(temp);
        }
        //设置物料总成本 MODULE_COST_PRICE_SUM
        procedure.setModuleCostPriceSum(sum);
        //修改标志 DESIGN_MODULE_TAG G002-1: 已提交
        procedure.setDesignModuleTag("G002-1");
        boolean flag = manufactureDesignProcedureService.updateById(procedure);
        return flag;
    }

    //查询工序设计单标志位已提交的
    @RequestMapping("queryProcessMaterialReview")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> queryProcessMaterialReview(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-1");
        wrapper.eq("CHANGE_TAG","B002-0");
        wrapper.eq("DESIGN_MODULE_TAG","G002-1");
        wrapper.eq("DESIGN_MODULE_CHANGE_TAG","G003-0");

        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureService.list(wrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> pageUtils=new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }


    //根据工序设计id查询工序设计单
    @RequestMapping("getIdQueryProcedure")
    @ResponseBody
    public ManufactureDesignProcedure getIdQueryProcedure(Integer id){
        ManufactureDesignProcedure procedure = manufactureDesignProcedureService.getById(id);
        return procedure;
    }

    //根据工序设计id查询工序明细
    @RequestMapping("getIdQueryDetails")
    @ResponseBody
    public List<ManufactureDesignProcedureDetails> getIdQueryDetails(Integer id){
        QueryWrapper<ManufactureDesignProcedureDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<ManufactureDesignProcedureDetails> detailsList = manufactureDesignProcedureDetailsService.list(wrapper);
        return detailsList;
    }

    //修改工序设计单的标志位
    @RequestMapping("updateProcedureTag")
    @ResponseBody
    public boolean updateProcedureTag(ManufactureDesignProcedure manufactureDesignProcedure){
        boolean flag=false;
        if(manufactureDesignProcedure.getDesignModuleTag().equals("G002-2")){
             flag = manufactureDesignProcedureService.updateById(manufactureDesignProcedure);
        }else {
             flag = manufactureDesignProcedureModuleService.removeProcedure(manufactureDesignProcedure);
        }
        return flag;
    }

    //查询所有工序设计单
    @RequestMapping("processAllInquiry")
    @ResponseBody
    public PageUtils<ManufactureDesignProcedure> processAllInquiry(DesignModuleVo vo,Integer pageNo,Integer pageSize){
        PageUtils<ManufactureDesignProcedure> manufactureList = manufactureDesignProcedureService.getAllManufactureList(vo, pageNo, pageSize);
        return manufactureList;
    }

    //根据产品编号查询工序明细
    @RequestMapping("getProductIdProcessDetails")
    @ResponseBody
    public List<ManufactureDesignProcedureDetails> getProductIdProcessDetails(String pid){
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",pid);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(wrapper);
        QueryWrapper<ManufactureDesignProcedureDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",one.getId());
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(queryWrapper);
        return list;
    }

    //根据工序明细id查询工序物料明细
    @RequestMapping("getDetailsIdQueryProduct")
    @ResponseBody
    public List<ManufactureDesignProcedureModule> getDetailsIdQueryProduct(Integer pid){
        QueryWrapper<ManufactureDesignProcedureModule> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",pid);
        List<ManufactureDesignProcedureModule> list = manufactureDesignProcedureModuleService.list(wrapper);
        return list;
    }

    //根据工序明细id查询工序名称和工序设计编号
    @RequestMapping("getIdQuery")
    @ResponseBody
    private List getIdQuery(Integer id){
        List<Object> list = new ArrayList<>();
        ManufactureDesignProcedureDetails details = manufactureDesignProcedureDetailsService.getById(id);
        list.add(details.getProcedureName());
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<>();
        wrapper.eq("id",details.getParentId());
        ManufactureDesignProcedure procedure = manufactureDesignProcedureService.getOne(wrapper);
        list.add(procedure.getDesignId());
        return list;
    }

    //查询可用数量
    @RequestMapping("selCount")
    @ResponseBody
    public List<DesignModuleDetails> selCount(String prodId){
        QueryWrapper<DesignModule> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",prodId);
        DesignModule module = designModuleService.getOne(wrapper);
        QueryWrapper<DesignModuleDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",module.getId());
        List<DesignModuleDetails> list = designModuleDetailsService.list(queryWrapper);
        return list;
    }
 }
